package com.example.photo_sharing.page.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.photo_sharing.R;
import com.example.photo_sharing.global.App;
import com.example.photo_sharing.tool.Impl.EditImpl;
import com.example.photo_sharing.tool.Impl.UserInfoSPImpl;
import com.example.photo_sharing.entity.FindListItem;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class listSavesAdapter extends RecyclerView.Adapter<listSavesAdapter.findViewHolder> {

    private listSavesAdapter.OnItemClickListener onItemClickListener;
    private Context context;
    private ArrayList<FindListItem> goodsEntities = new ArrayList<>();
    private App app;

    private UserInfoSPImpl usp;
    private SharedPreferences sharedPreferences;
    private String userId;
    private EditImpl edit;

    public listSavesAdapter(Context context, ArrayList<FindListItem> goodsEntities){
        //System.out.println(goodsEntities.size());
        app = new App();
        this.context = context;
        this.goodsEntities = goodsEntities;
        sharedPreferences = context.getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        usp = new UserInfoSPImpl(sharedPreferences);
        userId = usp.getUserId();
        edit = new EditImpl();
    }

    @Override
    public listSavesAdapter.findViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = View.inflate(context, R.layout.list_item_share_save,null);
        return new findViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(listSavesAdapter.findViewHolder holder, @SuppressLint("RecyclerView") int position) {
        FindListItem data = goodsEntities.get(position);

        holder.mItemTitle.setText(data.getTitle());
        Picasso.get().load(data.getImageUrlList().size()!=0 ?data.getImageUrlList().get(0):"1").into(holder.mItemImg);
        holder.mItemContent.setText(data.getContent()+"");

        //删除草稿的事件
        holder.mItemButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClickListener.OnDeleteClick(view,position,data);
                notifyItemRemoved(position);
                System.out.println(position);
                notifyDataSetChanged();
            }
        });

        //发布草稿的事件
        holder.bt_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClickListener.OnSendClick(view,position,data);
                notifyItemRemoved(position);
                System.out.println(position);
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return goodsEntities.size();
    }

    class findViewHolder extends RecyclerView.ViewHolder {

        //定义控件
        private ImageView mItemImg;
        private TextView mItemTitle;
        private Button mItemButton;
        private TextView mItemContent;
        private Button bt_send;

        public findViewHolder( View itemView) {
            super(itemView);
            //找到控件
            mItemImg = itemView.findViewById(R.id.save_item_img);
            mItemTitle = itemView.findViewById(R.id.save_item_title);
            mItemContent = itemView.findViewById(R.id.save_item_content);
            mItemButton = itemView.findViewById(R.id.del_button);
            bt_send = itemView.findViewById(R.id.send_button);

            //设置点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (onItemClickListener!=null){

                        onItemClickListener.OnItemClick(v,goodsEntities.get(getLayoutPosition()));
                    }
                }
            });

        }
    }

    //设置点击事件监听器
    public interface OnItemClickListener {
        void OnItemClick(View view, FindListItem data);
        void OnDeleteClick(View view,int position, FindListItem data);
        void OnSendClick(View view,int position, FindListItem data);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

}

